package com.huiyujiaoyu.powertime.ui.login;


import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.MainActivity;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.widget.LlkjToolBar;
import com.huiyujiaoyu.widget.SimpleTextWatcher;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class ImproveInfoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    @BindView(R.id.edt_content)
    EditText mEdtContent;
    @BindView(R.id.tv_boy)
    TextView mTvBoy;
    @BindView(R.id.tv_girl)
    TextView mTvGirl;
    @BindView(R.id.btn_enter)
    Button mBtnEnter;
    private String name;
    private int gender = 0;
    private TextView[] tvs;
    private Map<String,Object> params;
    @Override
    protected void initView() {
        setToolbar(mToolbar);
        mToolbar.setToolBar("完善个人资料", true, "", LlkjToolBar.isNull, false, "", LlkjToolBar.isNull);
    }


    @Override
    protected void initListener() {
        mEdtContent.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                setBtn();
            }
        });

    }


    private void setBtn(){
       mBtnEnter.setEnabled(!TextUtils.isEmpty(mEdtContent.getText()+"")&&gender!=0);
    }
    @Override
    protected void initData() {
        tvs = new TextView[2];
        tvs[0] = mTvBoy;
        tvs[1] = mTvGirl;
        setBtn();
    }



    @Override
    protected int getLayoutId() {
        return R.layout.activity_improve_info;
    }



    @OnClick({R.id.tv_boy, R.id.tv_girl,R.id.btn_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_boy:
                gender = 1;
                tvs[0].setSelected(true);
                tvs[1].setSelected(false);
                setBtn();
                break;
            case R.id.tv_girl:
                gender = 2;
                tvs[0].setSelected(false);
                tvs[1].setSelected(true);
                setBtn();
                break;
            case R.id.btn_enter:
                name = mEdtContent.getText()+"";
                if(params==null){
                    params = new HashMap<>();
                }
                params.put("gender",gender);
                params.put("nickname",name);
                params.put("province","");
                params.put("city","");
                params.put("intro","");
                HttpMethods.getInstance().update(mContext,getComp(),this,params);
                break;
        }
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_UPDATE:
                UserInfoBean.getInstance().setGender(gender+"");
                UserInfoBean.getInstance().setNickName(name);
                ActivityUtil.startActivity(mContext,MainActivity.class);
                break;
        }
    }
}
